package com.shop.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mall.common.Result;
import com.shop.mall.entity.User;
import com.shop.mall.mapper.UserMapper;
import com.shop.mall.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mall.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据登录名查询用户
     * @param loginName   登录名
     * @return   User
     */
    @Override
    public User findByLoginName(String loginName) {
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getLoginName, loginName);
        return userMapper.selectOne(userWrapper);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }

    /**
     * 查询所有用户信息
     * @return List<User>
     */
    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }

    /**
     *  分页查询用户信息
     * @param pageSize 每页显示的条数
     * @param currentPage  要查询的页
     * @param name  用户姓名
     * @return IPage<User>
     */
    @Override
    public IPage<User> pageList(int pageSize, int currentPage, String name) {
        // like 条件构造
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && !"".equals(name), User::getName, name);
        // 分页构造
        IPage<User> page = new Page<>(currentPage, pageSize);
        userMapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public Result<String> add(User user) {
        // 登录名校验
        if (user.getLoginName() == null || "".equals(user.getLoginName())) {
            return Result.error("登录名不能为空");
        }
        // 登录名重复校验
        if (findByLoginName(user.getLoginName()) != null) {
            return Result.error("登录名已存在，请重新输入");
        }
        if (user.getName() == null || "".equals(user.getName())) {
            return Result.error("姓名不能为空");
        }
        user.setPassword(Md5Util.getMD5String("123456"));
        if (userMapper.insert(user) > 0) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public boolean updateUser(User user) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(user.getPhone() != null && !"".equals(user.getPhone()), User::getPhone, user.getPhone())
        .set(user.getPassword() != null && !"".equals(user.getPassword()), User::getPassword, Md5Util.getMD5String(user.getPassword()) )
        .set(user.getSex() != null && !"".equals(user.getSex()), User::getSex, user.getSex())
        .set(user.getName() != null && !"".equals(user.getName()), User::getName, user.getName())
        .set(user.getMoney() != null , User::getMoney, user.getMoney())
                .eq(User::getId, user.getId());
        return userMapper.update(wrapper) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return userMapper.deleteById(id) > 0;
    }

}
